package com.gopay.scheduler.job;

import com.gopay.common.constants.deposit.PaymentFlagConstants;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.constants.txncd.IntTxnCd;
import com.gopay.common.cps.dao.deposit.DepositInfoQueryDAO;
import com.gopay.common.domain.cps.transfer.Trans00501VO;
import com.gopay.common.domain.cps.transfer.Trans00502VO;
import com.gopay.common.domain.cps.vo.CPSResponse;
import com.gopay.common.domain.deposit.CorpDepositInfo;
import com.gopay.common.exception.GopayException;
import com.gopay.remote.acps.service.AcctTransRemoteService;
import com.gopay.scheduler.GopayBaseJob;
import com.gopay.scheduler.common.CallCPSHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * 保证金上缴和返还定时任务
 * 
 * @DepositPayAndReturnJob.java
 * @author zhuliang
 * @2016-4- 12 下午3:39:35 www.gopay.com.cn Inc.All rights reserved.
 */
public class DepositPayAndReturnJob extends GopayBaseJob {
    // 日志
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource(name = "depositInfoQueryDAO")
    private DepositInfoQueryDAO depositInfoQueryDAO;


    @Resource(name = "callCPSHelper")
    private CallCPSHelper callCPSHelper;

    @Resource(name = "acctTransRemoteService")
    AcctTransRemoteService acctTransRemoteService;

    @Override
    public void doJobInternal() {
        //获取未缴清的数据
        List<CorpDepositInfo> list = depositInfoQueryDAO.getDepositListByCustId(PaymentFlagConstants.DEPOSIT_UNPAID.value);
        logger.error("保证金定时任务开始，查询待处理的记录数量="+ list.size());
        if (list != null && list.size() > 0) {
            int successCount = 0;

            // 调用CPS
            Object paramVo;
            CPSResponse response;
            CorpDepositInfo depositInfo;

            for (int i = 0; i < list.size(); i++) {
                depositInfo = list.get(i);
                    try {

                        paramVo = buildCallCpsParamVo(depositInfo);
                        // 调用cps
                        if (null != paramVo){
                            logger.error("保证金定时任务，调用cps，保证金信息表ID="+depositInfo.getSq()+"，custId="+depositInfo.getCustId());
                            response = callCPSHelper.callByVOTicket(paramVo);
                            if (!ProcCodeConstants.PROC_CODE_100S1000.equals(response.getRespCode())) {
                                logger.error("------调用cps返回异常  response=" + response.getRespCode());
                            }
                        }
                        successCount++;
                        logger.info("第" + successCount + "条处理成功,保证金信息表ID=" + depositInfo.getSq());

                    } catch (GopayException e) {
                        logger.error("保证金定时任务异常，保证金信息表ID="+depositInfo.getSq());
                        logger.error(e.getMessage());
                    } catch (Exception e) {
                        // 吃掉所有异常
                        logger.error("保证金定时任务异常", e);
                    }

            }
            logger.error("处理条数： " + successCount);
            logger.error("保证金定时任务结束");
        }
       
    }

    /**
     * 封装cps请求参数vo
     * @param depositInfo 订单信息
     * @return
     */
    private Object buildCallCpsParamVo(CorpDepositInfo depositInfo) {
        String id = String.valueOf(depositInfo.getSq());
        BigDecimal needPay = depositInfo.getPendingAmt();
        BigDecimal alreadyPay = depositInfo.getAlreadyAmt();
        BigDecimal payAmt = needPay.subtract(alreadyPay);

        Object obj = null;
        //交易权限开通 且 需缴-实缴>0
        if("0".equals(depositInfo.getDepositFlag())&&needPay.compareTo(alreadyPay)>0){
            logger.error("上缴保证金信息表ID="+depositInfo.getSq()+"，custId="+depositInfo.getCustId());
                Trans00501VO paramVo = new Trans00501VO();
                paramVo.setDtlSq(id);
                paramVo.setCustId(depositInfo.getCustId());
                paramVo.setOutAcct(depositInfo.getGnstAcct());
                paramVo.setInAcct(depositInfo.getDepositAcct());
                paramVo.setAmt(payAmt);
                paramVo.setTranCode(IntTxnCd._00501.value);
                paramVo.setRemark("上缴保证金");
                obj = paramVo;

         //上缴保证金交易权限关闭或者实缴大于需缴
        }
        /*else if("1".equals(depositInfo.getDepositFlag())
                ||alreadyPay.compareTo(needPay)>0){
            logger.error("返还保证金信息表ID="+depositInfo.getSq()+"，custId="+depositInfo.getCustId());
            //返还保证金
            Trans00502VO paramVo = new Trans00502VO();
            paramVo.setDtlSq(id);
            paramVo.setCustId(depositInfo.getCustId());
            paramVo.setOutAcct(depositInfo.getDepositAcct());
            paramVo.setInAcct(depositInfo.getGnstAcct());
            //当权限关闭时
            if("1".equals(depositInfo.getDepositFlag())){
                paramVo.setAmt(alreadyPay);
                //当下调保证金时
            }else{
                paramVo.setAmt(alreadyPay.subtract(needPay));
            }

            paramVo.setTranCode(IntTxnCd._00502.value);
            paramVo.setRemark("返还保证金");
            obj = paramVo;
        }*/

        return obj;
    }


}
